from mrjob.job import MRJob, MRStep
from mrjob.protocol import TextProtocol
import heapq
import datetime

class UserValueR(MRJob):

    # OUTPUT_PROTOCOL = TextProtocol

    def mapper(self, _, line):
        lineinfo = line.strip().split(',')
        if len(lineinfo) == 6:
            if lineinfo[3] == 'buy':
                final_day = datetime.datetime.strptime('2017-12-03', "%Y-%m-%d").date()
                now_day = datetime.datetime.strptime(lineinfo[4], "%Y-%m-%d").date()
                if 0 <= (final_day - now_day).days <= 1:
                    yield lineinfo[0], 4
                elif 2 <= (final_day - now_day).days <= 3:
                    yield lineinfo[0], 3
                elif 4 <= (final_day - now_day).days <= 5:
                    yield lineinfo[0], 2
                elif 6 <= (final_day - now_day).days <= 8:
                    yield lineinfo[0], 1
            else:
                yield lineinfo[0], 1

    def reducer(self, key, values):
        yield key, max(values)

    # def steps(self):
    #     return [
    #         MRStep(mapper=self.mapper_step1,
    #                reducer=self.reducer_step1)
    #     ]


if __name__ == '__main__':
    UserValueR.run()

